

import 'dart:io';

import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ncsjqz_getx/constant/constant.dart';
import 'package:flutter_ncsjqz_getx/page/login/login_logic.dart';
import 'package:flutter_ncsjqz_getx/widget/bottom_nav_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/not_network_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/willpop_widget.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';

import 'home_logic.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final logic = Get.put(HomeLogic());
  LoginLogic loginLogic = Get.put(LoginLogic());
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    logic.init();
    showReadDialog();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<HomeLogic>(
      builder: (controller) {
        return willPopWidget(
          appName: "农场APP",
          child: Scaffold(
            body:  logic.bodyData[logic.currentPageIndex],
            bottomNavigationBar: bottomNavWidget(
              currentIndex: logic.currentPageIndex,
              menuData: logic.menuData,
              onTap: (v) {
                logic.setIndex(v);
              },
            ),
            bottomSheet: Constant.netStatus=="ConnectivityResult.none" ? notNetworkWidget() :  SizedBox(),
          ),
        );
      },
    );
  }


  showReadDialog(){
    ScrollController scrollController = ScrollController();
    if(SpUtil.getBool("start_dialog") ?? false)return;
    WidgetsBinding.instance!.addPostFrameCallback((timeStamp) async {
      Get.dialog(
        WillPopScope(
          child: Material(
            color: Colors.transparent,
            child: Center(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    width: Get.width * 0.7,
                    height: Get.width,
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(10),
                    ),
                    padding: EdgeInsets.symmetric(horizontal: 15),
                    child: Column(
                      children: [
                        Text("\n温馨提示\n",style: TextStyle(fontWeight: FontWeight.bold,fontSize: 16),),
                        Expanded(
                            child: CupertinoScrollbar(
                              controller: scrollController,
                              isAlwaysShown: true,
                              thickness: 5,
                              child: ListView(
                                controller: scrollController,
                                shrinkWrap: true,
                                children: [
                                  Text("尊敬的用户您好，欢迎使用 绿农生态APP。\n\n"
                                      "绿农生态深知个人信息对您的重要性，并会尽全力保护您的个人信息安全可靠。\n\n"
                                      "我们致力于维持您对我们的信任，恪守以下原则，保护您的个人信息：\n\n"
                                      "权责一致原则、目的明确原则、选择同意原则、最少够用原则、确保安全原则、主体参与原则、公开透明原则等。\n\n"
                                      "同时，我们将按业界成熟的安全标准，采取相应的安全保护措施来保护您的个人信息。\n\n"
                                      "为了便于您了解您在使用我们的服务时，我们需要收集的信息类型与用途，我们将结合具体服务向您逐一说明。\n\n"
                                      "请在使用我们的产品（或服务）前，仔细阅读并了解《隐私协议》和《用户协议》（尤其是加粗及下划线部分）。\n\n"
                                      "注意：同意时 需要用到存储权限 存储同意状态，请允许，同意后 下次启动将不再弹出此框。\n\n"
                                      "如不同意我们的《隐私协议》和《用户协议》 请点击暂不使用并退出APP。"
                                    ,style: TextStyle(fontSize: 14,height: 1.3),),
                                ],
                              ),
                            )
                        ),
                        Row(
                          children: [
                            Text("\n请阅读",style: TextStyle(fontSize: 14,color: Colors.grey),),
                            InkWell(
                              onTap: (){
                                loginLogic.getProtocol("隐私政策");
                                // Get.to(HtmlWidget(
                                //   content:  '${controller.data2?.content}',
                                //   title: '隐私协议',
                                // ),preventDuplicates: false);
                              },
                              child: Text("\n《隐私协议》",style: TextStyle(fontSize: 14,color: Colors.green),),
                            ),
                            Text("\n和",style: TextStyle(fontSize: 14,color: Colors.grey),),
                            InkWell(
                              onTap: (){
                                loginLogic.getProtocol("用户协议");
                                // Get.to(HtmlWidget(
                                //   content:  '${controller.data1?.content}',
                                //   title: '用户协议',
                                // ),preventDuplicates: false);
                              },
                              child: Text("\n《用户协议》",style: TextStyle(fontSize: 14,color: Colors.green),),
                            ),
                          ],
                        ),
                        Container(
                          padding: EdgeInsets.symmetric(vertical: 15),
                          height: 60,
                          child: Row(
                            children: [
                              Expanded(child: MaterialButton(
                                onPressed: () async{
                                  if(!await Permission.storage.request().isGranted) return;
                                  SpUtil.putBool("start_dialog",true);
                                  Get.back();
                                },
                                child: Text("同意",style: TextStyle(fontSize: 14,color: Colors.green ),),
                              ),flex: 1,),
                              Expanded(child: MaterialButton(
                                onPressed: (){
                                  exit(0);
                                },
                                child: Text("暂不使用并退出APP",style: TextStyle(fontSize: 14,color: Colors.red),),
                              ),flex: 2,),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
          onWillPop: ()async=>false,
        ),
        barrierDismissible: false,
      );
    });
  }

  @override
  void dispose() {
    Get.delete<LoginLogic>();
    Get.delete<HomeLogic>();
    super.dispose();
  }
}